﻿using MySql.Data.MySqlClient;
using SGV.Datos.Interfases;
using SGV.Entidad;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using DapperExtensions;

namespace SGV.Datos.Repositories
{
    public class DepositoRepository: IDepositoRepository
    {

        private IDbConnection _Db;

        public DepositoRepository()
        {
            //constructor
        }


        public IList<Deposito> FindAll()
        {
            string query = @"SELECT id, denominacion, direccion, telefono, email,
                                    descripcion, fechaalta FROM deposito";

            using (_Db = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ToString()))
            {
                return _Db.Query<Deposito>(query).ToList();
            }
        }

        public IQueryable<Deposito> Find(Expression<Func<Deposito, bool>> predicate)
        {
            throw new NotImplementedException();
        }

        public Deposito FindById(int id)
        {
            throw new NotImplementedException();
        }

        public int add(Deposito newEntity)
        {
            string query = "INSERT INTO deposito (denominacion, direccion, telefono," +
                                                "email, descripcion, fechaalta) VALUES" +
                                                "(@denominacion, @direccion, @telefono, " +
                                                "@email, @descripcion, @fechaalta);" +
                                        "SELECT CAST(LAST_INSERT_ID() AS UNSIGNED INTEGER);";

            using (_Db = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ToString()))
            {
                var id = _Db.Query<ulong>(query, new
                {
                    newEntity.denominacion,
                    newEntity.direccion,
                    newEntity.telefono,
                    newEntity.email,
                    newEntity.descripcion,
                    newEntity.fechaalta,

                }).Single();

                return Convert.ToInt32(id);
            }

        }

        public bool remove(int id)
        {
            throw new NotImplementedException();
        }

        public int Modify(Deposito entity)
        {
            throw new NotImplementedException();
        }
    }
}
